package tsing.javase.thread.futher;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @Author Tsing/吴清平
 * @Date 2022/10/20 16:02
 * @Version 1.0
 *
 * 批量接收结果
 */
public class MultiFutrures {


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(20);
        ArrayList<Future> futures = new ArrayList<>(); //list 集合保存线程执行结果
        for (int i = 0; i < 20; i++) {
            Future<Integer> future = service.submit(new CallableTask());
            futures.add(future);
        }

        Thread.sleep(5000);
        for (Future future : futures) {
            System.out.println(future.get());
        }


        service.shutdown(); //关闭线程池

    }

    static class CallableTask implements Callable <Integer> {
        @Override
        public Integer call() throws Exception {
            Thread.sleep(1000);
            return new Random().nextInt();
        }
    }



}
